# -*- coding: utf-8 -*-

import views.books
import models.books

class BooksController():

    # @type view views.books.BooksView
    view = None
    model = None
    activeBookId = None
    notesController = None

    def __init__(self, frame):
        self.view = views.books.BooksView(self, frame)
        self.model = models.books.BooksModel()
    
    def setNotesController(self, controller):
        self.notesController = controller

    def initAction(self):
        self.view.hideBooksPanel()

    def newAction(self, event):
        self.activeBookId = None
        self.notesController.setActiveBookId(None)
        self.view.resetBooksPanel()
        self.view.showBooksPanel()
        self.view.focusBooksPanel()
        self.view.hideNotesPanel()
        self.view.enableGeneralPanel(True)

    def saveAction(self, event, focus = True, bookData = None):
        if bookData is None:
            bookData = self.view.getBookBasicData()

        if self.activeBookId is not None:
            bookData["id"] = self.activeBookId;

        if len(bookData["name"]) == 0:
            self.view.showError(u"Název publikace nebyl vyplněn")
            return
        elif len(bookData["name"]) > 30:
            self.view.showError(u"Název publikace může mít maximálně 30 znaků")
            return

        self.view.setStatus(u"Ukládám publikaci...")
        result = self.model.saveBook(bookData)
        if result is True:
            self.view.setStatus(u"Hotovo")
            self.refreshBookListsAction()
            self.refreshNoteBookListsAction()
            self.refreshTagsListAction()
            if focus is True:
                self.editAction(bookData["id"])
        else:
            self.view.setStatus(u"Při ukládání došlo k chybě")
            self.view.showError(u"Při ukládání došlo k chybě")

    def refreshBookListsAction(self, event = None, tags = None):
        self.view.setStatus(u"Obnovuji seznam publikací...")
        if tags is not None and len(tags):
            books = self.model.getBooksByTags(tags)
        else:
            books = self.model.getBooksList()
        self.view.updateBookLists(books)
        self.view.setStatus(u"Hotovo")

    def refreshNoteBookListsAction(self, event = None, tags = None):
        self.view.setStatus(u"Obnovuji seznam publikací...")
        if tags is not None and len(tags):
            books = self.model.getBooksByTags(tags)
        else:
            books = self.model.getBooksList()
        self.view.updateNoteBookLists(books)
        self.view.setStatus(u"Hotovo")

    def refreshTagsListAction(self):
        tags = self.model.getTagsList()
        self.view.updateTagsLists(tags)

    def deleteAction(self, event = None):
        bookIDs = self.view.getSelectedBookIds()
        if len(bookIDs) == 0:
            self.view.setStatus(u"Nebyly vybrány žádné publikace")
            return
        books = self.model.getBooksByIds(bookIDs)
        if self.view.showDeleteConfirmDialog(books) is True:
            for book in books.itervalues():
                self.view.setStatus(u"Mažu publikaci '%s'..." % (book["name"]))
                if self.model.deleteBook(book["id"]) is not True:
                    self.view.showError(u"Při mazání publikace '%s' došlo k chybě" % (book["name"]))
                if self.activeBookId == book["id"]:
                    self.view.hideBooksPanel()

                if self.notesController.activeBookId == book["id"]:
                    self.notesController.view.hideNotePanel()

            self.syncAction()
            self.notesController.syncAction()
            self.view.setStatus(u"Hotovo")

    def editAction(self, bookID):
        book = self.model.getBookById(bookID)
        if book is not None:
            self.view.resetBooksPanel()
            self.view.setActiveBook(book)
            self.view.showBooksPanel(book["name"])
            self.view.focusBooksPanel()
            self.activeBookId = bookID
            self.notesController.setActiveBookId(bookID)
            self.notesController.refreshNoteListsAction()
            self.view.showNotesPanel()

            if book["id"] is self.model.getDefaultBookId():
                self.view.enableGeneralPanel(False)
            else:
                self.view.enableGeneralPanel(True)

    def syncAction(self, event = None):
        self.view.setStatus(u"Provádím synchronizaci publikací...")
        self.model.synchronize()
        self.refreshBookListsAction(event)
        self.refreshNoteBookListsAction(event)
        self.refreshTagsListAction()

    def createDefaultBookAction(self):
        book = {}
        book["name"] = u"- nezařazené -"
        book["author"] = ""
        book["genre"] = ""
        book["isbn"] = ""
        book["descr"] = u"Knihy nezařazené k žádné publikaci"
        book["tags"] = u"- nezařazené -"

        self.saveAction(None, False, book)